﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OpenCvSharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private Mat _image = Cv2.ImRead(@"img.png", ImreadModes.Grayscale);
        private CvDrawObj.CvDrawRotatedRect _roi = new CvDrawObj.CvDrawRotatedRect(new CvRotatedRect(809.333333333333, 126.666666666667,2.28267873323278, 1445.59724358774, 100));
        private CvDrawObj.CvDrawRing _roi1 = new CvDrawObj.CvDrawRing(new CvCircle(825.333333333333, 601.333333333333, 288.548859717803), 227.07795481809288) { AngleEnabled = true,SweepAngle=270 };
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            cvControl1.DispImage(_image);

            //cvControl1.DispObj(new CvDispObj.CvDispCircle(new CvCircle(50, 50, 30)) { IsControlCoordSystem = true });
            //cvControl1.DispObj(new CvDispObj.CvDispCross(new Point2d(50, 50), 30) { IsControlCoordSystem = false });
            //cvControl1.DispObj(new CvDispObj.CvDispLine(new CvLine(50, 70, 150, 170)) { DisplayArrow = true });
            //cvControl1.DispObj(new CvDispObj.CvDispRing(new CvCircle(120, 50, 20), 10) { StartAngle = 0d, SweepAngle = 270d });
            //cvControl1.DispObj(new CvDispObj.CvDispRect(new Rect2d(10, 10, 200, 100)));
            //cvControl1.DispObj(new CvDispObj.CvDispRotatedRect(new CvRotatedRect(190, 50, 5, 50, 30)) { DisplayArrow = true, DisplayCross = true, ArrowInsideMode = false });
            //cvControl1.DispObj(new CvDispObj.CvDispText("this is message", new Point2d(150, 50)) { Border = true });

            //cvControl1.DrawObj(new CvDrawObj.CvDrawLine(new CvLine(100, 100, 300, 300)));
            //cvControl1.DrawObj(new CvDrawObj.CvDrawCircle(new CvCircle(300, 300, 100)) { AngleEnabled = true });
            //cvControl1.DrawObj(new CvDrawObj.CvDrawRing(new CvCircle(300, 300, 100), 50) { AngleEnabled = true });
            //cvControl1.DrawObj(new CvDrawObj.CvDrawRect(new Rect2d(300, 300, 100, 50)));
            //cvControl1.DrawObj(new CvDrawObj.CvDrawRotatedRect(new CvRotatedRect (500, 500, 45, 200, 100)));

            if (_opMode != 3)
            {
                cvControl1.DrawObj(_roi);
            }
            else
            {
                cvControl1.DrawObj(_roi1);
            } 
        }

        private int _opMode = 2;
        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);

            switch (_opMode)
            {
                case 0:
                    cvControl1.DispClear(true);
                    cvControl1.DrawObj(_roi);
                    break;
                case 1:
                    cvControl1.DispClear(true);
                    List<CvPoint> result =  CvMeasure.MeasurePos(_image, _roi.RotatedRect, 1d, 30d, CvTransition.Negative, CvSelect.First);
                    for (int i = 0; i < result.Count; i++)
                    {
                        cvControl1.DispObj(new CvDispObj.CvDispCross(result[i]));
                    }
                    break;
                case 2:
                    cvControl1.DispClear(true);
                    List<CvDispObj> resultPoints = new List<CvDispObj>();
                    List<CvDispObj> resultCalipers = new List<CvDispObj>();
                    CvLine resultLine =  CvMeasure.FindLine(_image,_roi.RotatedRect,10,10d,1d,30d, CvTransition.Positive, CvSelect.First,resultPoints,resultCalipers);

                    cvControl1.DispObj(resultPoints);
                    cvControl1.DispObj(resultCalipers);
                    cvControl1.DispObj(new CvDispObj.CvDispLine(resultLine) { Color = Color.Lime, PenWidth = 2f }) ;
                    break;
                case 3:
                    cvControl1.DispClear(true);
                    List<CvDispObj> resultPoints1 = new List<CvDispObj>();
                    List<CvDispObj> resultCalipers1 = new List<CvDispObj>();
                    CvCircle resultCircle = CvMeasure.FindCircle(_image, _roi1.Circle,_roi1.StartAngle,_roi1.SweepAngle, 10, 10d, _roi1.RingWidth,1d, 30d, CvDirection.Forward, CvTransition.Positive, CvSelect.First, resultPoints1, resultCalipers1);

                    cvControl1.DispObj(resultPoints1);
                    cvControl1.DispObj(resultCalipers1);
                    cvControl1.DispObj(new CvDispObj.CvDispCircle(resultCircle) { Color = Color.Lime, PenWidth = 2f });
                    break;
                case 4:
                    cvControl1.DispClear(true);
                    using (Mat region = CvBlob.Threshold(_image, 0, 100, true))
                    using (Mat back = new Mat(region.Size(),MatType.CV_8UC3,Scalar.White))
                    {
                        Mat[] regions = CvBlob.Connection(region);
                        //Mat[] sels = CvBlob.SelectShape(regions, true, new string[] { "area" }, new double[] { 8617 - 10000 }, new double[] { 8617 + 1000000 });
                    }
                    break;
                default:
                    break;
            }


        }
    }
}
